<template>
  <div class="edit-suppliers">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>设置</el-breadcrumb-item>
      <el-breadcrumb-item :to="{ path: '/supply_setting' }">供应设置</el-breadcrumb-item>
      <el-breadcrumb-item>编辑供应商</el-breadcrumb-item>
    </el-breadcrumb>
    <el-form :model="ruleForm"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm">
      <div class="information-page">
        <el-form-item class="information-title">
          <h1>基础信息</h1>
        </el-form-item>
        <div class="basic-information">
          <div class="information-left-page">
            <el-form-item label="公司名称"
              :required="true">
              <el-input v-model="ruleForm.supplierBaseInfo.company"
                placeholder="请输入公司名称"></el-input>
            </el-form-item>
            <el-form-item label="公司类型"
              :required="true">
              <el-select v-model="ruleForm.supplierBaseInfo.type"
                placeholder="请选择公司类型">
                <el-option label="企业"
                  :value="0"></el-option>
                <el-option label="旅行社"
                  :value="1"></el-option>
                <el-option label="酒店"
                  :value="2"></el-option>
                <el-option label="第三方"
                  :value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="关联酒店"
              :required="true">
              <el-select v-model="ruleForm.hotelId">
                <el-option v-for="item in hotelNames"
                  :key="item.id"
                  :value="item.id"
                  :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="区域"
              class="area"
              :required="true">
              <el-cascader :options="cities"
                :props="{ value: 'cityid', label: 'city', children: 'citiess' }"
                v-model="city"
                filterable
                change-on-select>
              </el-cascader>
            </el-form-item>
            <el-form-item label="邮编">
              <el-input v-model="ruleForm.supplierBaseInfo.postcode"
                placeholder="请输入邮编"></el-input>
            </el-form-item>
            <el-form-item label="公司网址">
              <el-input v-model="ruleForm.supplierBaseInfo.url"
                placeholder="请输入公司网址"></el-input>
            </el-form-item>
            <el-form-item label="公司简介">
              <el-input type="textarea"
                v-model="ruleForm.supplierBaseInfo.profile"
                :rows="4"
                placeholder="请输入公司简介"></el-input>
            </el-form-item>
          </div>
          <div class="information-right-page">
            <el-form-item label="公司logo"
              style="margin-bottom: 28px;">
              <el-upload class="avatar-uploader"
                :show-file-list="false"
                :http-request="sendChange"
                action="https://jsonplaceholder.typicode.com/posts/">
                <img v-if="oldPath"
                  :src="oldPath"
                  class="avatar">
                <i v-else
                  class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
              <p class="img-content">仅支持JPG, JPEG, BMP, PNG格式, 文件小于10M<br />(建议使用缕空白色图像, 最佳尺寸126 * 45px)</p>
            </el-form-item>
            <el-form-item label="详细地址"
              :required="true">
              <el-input v-model="ruleForm.supplierBaseInfo.address"
                placeholder="请输入公司详细地址"></el-input>
            </el-form-item>
            <div style="overflow: hidden;">
              <div class="information-left">
                <el-form-item label="电话"
                  :required="true">
                  <el-input v-model="ruleForm.supplierBaseInfo.landline"
                    placeholder="请输入公司电话号码"></el-input>
                </el-form-item>
              </div>
              <div class="information-right">
                <el-form-item label="传真">
                  <el-input v-model="ruleForm.supplierBaseInfo.fax"
                    placeholder="请输入公司传真"></el-input>
                </el-form-item>
              </div>
            </div>
            <el-form-item label="邮箱">
              <el-input v-model="ruleForm.supplierBaseInfo.email"
                placeholder="请输入联系人邮箱"></el-input>
            </el-form-item>
          </div>
        </div>
        <el-form-item class="information-title">
          <h1>财务信息</h1>
        </el-form-item>
        <div class="financial-information">
          <div class="information-left-page">
            <el-form-item label="开户银行">
              <el-input v-model="ruleForm.financeInfo.banks"
                placeholder="请输入开户银行"></el-input>
            </el-form-item>
            <el-form-item label="银行账号">
              <el-input v-model="ruleForm.financeInfo.banksAccount"
                placeholder="请输入公司银行账号"></el-input>
            </el-form-item>
            <el-form-item label="发票抬头">
              <el-input v-model="ruleForm.financeInfo.invoice"
                placeholder="请输入发票抬头"></el-input>
            </el-form-item>
            <el-form-item label="开户支行">
              <el-input v-model="ruleForm.financeInfo.branch"
                placeholder="请输入开户支行名称"></el-input>
            </el-form-item>
          </div>
          <div class="information-right-page">
            <el-form-item label="纳税人识别号">
              <el-input v-model="ruleForm.financeInfo.taxCode"
                placeholder="请输入纳税人识别号"></el-input>
            </el-form-item>
            <el-form-item label="结算方式"
              :required="true">
              <el-select v-model="ruleForm.financeInfo.clearingMode"
                placeholder="请选择结算方式">
                <el-option label="日结"
                  :value="0"></el-option>
                <el-option label="周结"
                  :value="1"></el-option>
                <el-option label="月结"
                  :value="2"></el-option>
                <el-option label="半月结"
                  :value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="结算时间"
              :required="true"
              v-if="ruleForm.financeInfo.clearingMode !== 0">
              <el-select v-model="ruleForm.financeInfo.clearingTime"
                placeholder="请选择结算时间"
                v-if="ruleForm.financeInfo.clearingMode === 1">
                <div v-for="item in timeData">
                  <el-option :label="item.label"
                    :value="item.value">{{item.label}}</el-option>
                </div>
              </el-select>
              <el-select v-model="ruleForm.financeInfo.clearingTime"
                placeholder="请选择结算时间"
                v-if="ruleForm.financeInfo.clearingMode === 2">
                <div v-for="item in timeData">
                  <el-option :label="item"
                    :value="item"
                    v-if="ruleForm.financeInfo.clearingMode === 2">{{item}}</el-option>
                </div>
              </el-select>
              <div class="time-style"
                v-if="ruleForm.financeInfo.clearingMode === 3">
                <div class="items">
                  <el-select v-model="ruleForm.financeInfo.clearingTime"
                    placeholder="请选择结算时间">
                    <div v-for="item in timeData">
                      <el-option :label="item"
                        :value="item">{{item}}</el-option>
                    </div>
                  </el-select>
                </div>
                <div class="item">
                  <el-select v-model="ruleForm.financeInfo.clearingTimeTwo"
                    placeholder="请选择结算时间">
                    <div v-for="item in timeData">
                      <el-option :label="item"
                        :value="item">{{item}}</el-option>
                    </div>
                  </el-select>
                </div>
              </div>
            </el-form-item>
          </div>
        </div>
        <el-form-item class="information-title">
          <h1>合作信息</h1>
        </el-form-item>
        <div class="service-support">
          <div class="information-left-page">
            <div style="overflow: hidden;">
              <div class="information-left">
                <el-form-item label="签约人"
                  :required="true">
                  <el-input v-model="ruleForm.contractInfo.contractor"
                    placeholder="请输入签约人姓名"></el-input>
                </el-form-item>
              </div>
              <div class="information-right">
                <el-form-item label="手机"
                  :required="true">
                  <el-input v-model="ruleForm.contractInfo.contractorPhone"
                    type="number"
                    placeholder="请输入签约人手机号码"></el-input>
                </el-form-item>
              </div>
              <div class="information-left">
                <el-form-item label="账号"
                  :required="true">
                  <el-input v-model="ruleForm.contractInfo.account"
                    placeholder="请输入账号名称"
                    disabled></el-input>
                </el-form-item>
              </div>
              <div class="information-right">
                <el-form-item label="角色"
                  :required="true">
                  <el-select v-model="roleName"
                    placeholder="请选择角色">
                    <el-option :label="roleName"
                      :value="roleName"></el-option>
                  </el-select>
                </el-form-item>
              </div>
            </div>
          </div>
          <div class="information-right-page">
            <div style="overflow: hidden;">
              <div class="information-left">
                <el-form-item label="联系人"
                  :required="true">
                  <el-input v-model="ruleForm.contractInfo.userName"
                    placeholder="请输入联系人"></el-input>
                </el-form-item>
              </div>
              <div class="information-right">
                <el-form-item label="联系人手机"
                  :required="true">
                  <el-input v-model="ruleForm.contractInfo.userPhone"
                    type="number"
                    placeholder="请输入联系人手机"></el-input>
                </el-form-item>
              </div>
              <div class="information-left">
                <el-form-item label="职位">
                  <el-input v-model="ruleForm.contractInfo.position"
                    placeholder="请输入联系人职位"></el-input>
                </el-form-item>
              </div>
              <div class="information-right">
                <el-form-item label="QQ">
                  <el-input v-model="ruleForm.contractInfo.qq"
                    placeholder="请输入QQ号码"></el-input>
                </el-form-item>
              </div>

            </div>
          </div>

        </div>
      </div>
      <el-form-item label="合作模式"
        required>
        <el-radio-group v-model="ruleForm.contractInfo.modeId">
          <el-radio v-for="item in modeList"
            :key="item.id"
            :label="item.id">{{item.name}}</el-radio>
        </el-radio-group>
      </el-form-item>
      <div class="information-btn">
        <el-form-item>
          <el-button type="primary"
            @click="saveForm">保存</el-button>
          <el-button @click="back">取消</el-button>
          <el-button @click="resetPassword">重置密码</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>
<script>
import {
  getSuppliersInformation,
  getRoleList,
  resetPassword,
  updateSuppliers,
  getHotelsList,
  logo
} from "@/public/js/setting";
import { getModeList } from "@/public/js/mode";
const ButtonFooter = () => import("@/components/common/ButtonFooter");
const cityData = require("@/data/editCities.json");
export default {
  name: "CompanyInformation",
  data() {
    return {
      timeType: false,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        }
      },
      modeList: [],
      region: "",
      timeWeek: "",
      fileList: [],
      cities: cityData.provincesEntities,
      ruleForm: {
        supplierBaseInfo: {
          logo: ""
        },
        financeInfo: {},
        contractInfo: {
          suppliersContracts: [],
          modeId: 1
        },
        hotelId: ""
      },
      files: false,
      clearingTime: true,
      timeData: [
        { label: "周日", value: 7 },
        { label: "周一", value: 1 },
        { label: "周二", value: 2 },
        { label: "周三", value: 3 },
        { label: "周四", value: 4 },
        { label: "周五", value: 5 },
        { label: "周六", value: 6 }
      ],
      city: [],
      roleData: [],
      contractFile: "",
      userId: null,
      roleName: "供应商管理员",
      hotelNames: [],
      oldPath: ""
    };
  },
  created() {
    this._getHotelsList();
    getSuppliersInformation().then(res => {
      if (res.data.errCode === 0) {
        this.ruleForm.contractInfo = res.data.contractInfo;

        this.ruleForm.financeInfo = res.data.financeInfo;
        this.ruleForm.supplierBaseInfo = res.data.supplierBaseInfo;
        this.ruleForm.hotelId = res.data.hotelId;
        this.oldPath = res.data.supplierBaseInfo.logo;
        this.city = res.data.supplierBaseInfo.area;
        this.userId = res.data.contractInfo.userId;
        if (res.data.contractInfo.contractFiles.length > 0) {
          res.data.contractInfo.contractFiles.forEach(item => {
            this.fileList.push({ name: item.fileName, url: item.fileId });
          });
        }
        if (this.fileList.length) {
          if (this.fileList.length > 0) this.files = true;
        } else {
          this.files = false;
        }
        this._getModeList();
      } else {
      }
    });
    getRoleList().then(res => {
      if (res.data.errCode === 0) this.roleData = res.data.roleEntities;
    });
  },
  computed: {
    modeData() {
      return this.ruleForm.financeInfo.clearingMode;
    }
  },
  watch: {
    modeData(value) {
      if (value === 0) {
        this.clearingTime = false;
      } else if (value === 1) {
        this.clearingTime = true;
        this.timeData = [
          { label: "周日", value: 7 },
          { label: "周一", value: 1 },
          { label: "周二", value: 2 },
          { label: "周三", value: 3 },
          { label: "周四", value: 4 },
          { label: "周五", value: 5 },
          { label: "周六", value: 6 }
        ];
      } else {
        this.timeData = 31;
        this.clearingTime = true;
      }
    }
  },
  methods: {
    // 获取酒店列表
    _getHotelsList() {
      getHotelsList({
        id: sessionStorage.getItem("SuppliersId")
      }).then(res => {
        this.hotelNames = res.data.hotelNames;
      });
    },
    //获取mode
    _getModeList() {
      getModeList().then(res => {
        this.modeList = res.data.modes;
        if (!this.ruleForm.contractInfo.modeId) {
          this.ruleForm.contractInfo.modeId = this.modeList[0].id;
        }
      });
    },
    resetPassword() {
      resetPassword(this).then(res => {
        if (res.data.errCode === 0) {
          this.$alert("成功!  密码重置为 888888", "提示", {
            confirmButtonText: "确定",
            callback: action => {}
          });
        }
      });
    },
    dowloadFiles() {
      const evt = new MouseEvent("click");
      this.fileList.forEach(item => {
        this.$refs.downloadFileParent.download = item.url;
        this.$refs.downloadFileParent.href = item.url;
        this.$refs.downloadFileParent.dispatchEvent(evt);
      });
    },
    addServiceInpput() {
      this.ruleForm.contractInfo.suppliersContracts.push({
        room: "",
        floorPrice: "",
        roomAmount: "",
        breakfast: ""
      });
    },
    deleteInput(index) {
      this.ruleForm.contractInfo.suppliersContracts.splice(index, 1);
    },
    handleRemove(file, fileList) {
      this.fileList = fileList;
      if (this.fileList.length) {
        if (this.fileList.length > 0) this.files = true;
      } else {
        this.files = false;
      }
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件，本次选择了 ${
          files.length
        } 个文件，共选择了 ${files.length + fileList.length} 个文件`
      );
    },
    saveForm() {
      this.ruleForm.supplierBaseInfo.area = parseInt(
        this.city[this.city.length - 1]
      );
      if (this.ruleForm.financeInfo.clearingMode === 0)
        this.ruleForm.financeInfo.clearingTime = 0;
      this.ruleForm.contractInfo.contractFiles = [];
      if (this.fileList.length > 0) {
        this.fileList.forEach(item => {
          this.ruleForm.contractInfo.contractFiles.push({
            fileId: item.url,
            fileName: item.name
          });
        });
      }
      this.ruleForm.supplierBaseInfo.logo = this.oldPath;
      updateSuppliers(this.ruleForm).then(res => {
        if (res.data.errCode === 0) {
          this.$message.success("编辑供应商成功");
          this.$router.go(-1);
        }
      });
    },
    back() {
      this.$router.go(-1);
    },
    sendChange(e) {
      var reader = new FileReader();
      reader.readAsDataURL(e.file);
      reader.onload = e => {
        let photoPath = e.target.result;
        logo({
          nowPath: photoPath,
          oldPath: this.oldPath
        }).then(res => {
          this.oldPath = res.data.path;
        });
      };
    },
    // sendChange(req) {
    //   getQiniuPublicToken(this)
    //     .then(res => {
    //       if (res.data.errCode === 0) return res.data.qnInfo;
    //     })
    //     .then(res => {
    //       const formData = new FormData();
    //       formData.append("file", req.file);
    //       formData.append("token", res);
    //       formData.append("key", window.commo.uuid + "_" + this.file.name);
    //       updateQiniu(formData).then(res => {
    //         this.ruleForm.supplierBaseInfo.logo =
    //           window.commo.qiniuImageUrl + res.data.key;
    //       });
    //     });
    //   console.log(req);
    // },
    onFilesAdd(file, filelist) {
      this.file = file;
    },
    downloadChange(req) {
      this.file.name = req.file.name;
      getQiniuPublicToken(this)
        .then(res => {
          if (res.data.errCode === 0) return res.data.qnInfo;
        })
        .then(res => {
          const formData = new FormData();
          formData.append("file", req.file);
          formData.append("token", res);
          formData.append("key", window.commo.uuid + "_" + req.file.name);
          this.fileList = [];
          updateQiniu(formData).then(res => {
            if (res.status === 200) {
              this.fileList.push({
                name: res.data.key,
                url: window.commo.qiniuImageUrl + res.data.key
              });
              if (this.fileList.length) {
                if (this.fileList.length > 0) this.files = true;
              } else {
                this.files = false;
              }
            }
          });
        });
    },
    downloadFilesAdd(file, filelist) {
      this.file = file;
    }
  },
  components: {
    ButtonFooter
  }
};
</script>
<style lang="scss" scoped>
.time-style {
  display: flex;
  .item,
  .items {
    flex: 1;
  }
  .items {
    margin-right: 10px;
  }
}
.download-file {
  position: absolute;
  top: 0;
  left: 30%;
  color: #409eff;
  z-index: 9999;
  font-size: 12px;
}
.edit-suppliers {
  margin-bottom: 63px;
  .el-form {
    margin-top: 10px;
    background-color: white;
    overflow: auto;
    .information-page {
      padding: 20px;
      .basic-information,
      .financial-information,
      .service-support {
        overflow: hidden;
        .information-left-page {
          width: 47%;
          float: left;
        }
        .information-right-page {
          width: 47%;
          float: right;
        }
      }
      .basic-information {
        .information-left-page {
          .information-title {
            .el-form-item__content {
              margin-left: 0 !important;
            }
          }
          .area {
            text-align: left;
          }
        }
      }
      h1 {
        color: #45a2ff;
        text-align: left;
      }
    }
  }
}

.information-left {
  width: 50%;
  float: left;
}
.information-right {
  width: 50%;
  float: left;
}
.service_person {
  width: 100%;
  height: 40px;
  background-color: #eef1f9;
  margin-bottom: 10px;
  .item-first {
    width: 40% !important;
  }
  .item {
    width: 20%;
    float: left;
    text-align: left;
    label {
      font-size: 12px;
      margin-left: 10px;
    }
  }
}
.service-input {
  margin-bottom: 10px;
  display: inline-block;
  position: relative;
  .item-input {
    width: 40% !important;
  }
  .input-delete {
    position: absolute;
    top: 0;
    right: -25px;
  }
  .el-input {
    width: 20%;
    float: left;
    text-align: left;
    .el-input__inner {
      width: 90%;
    }
  }
  .last-input {
    .el-input__inner {
      width: 100%;
    }
  }
}
.service-btn {
  text-align: left;
}
.el-select,
.el-cascader {
  display: block;
}

.el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100%;
}

.information-left-page {
  .el-upload {
    display: block;
    text-align: left;
    .el-button + .el-button {
      margin-left: 30%;
    }
  }
}
.el-upload-list {
  position: absolute;
  top: 0;
  left: 0;
  width: 30%;
}
</style>
